package com.tansun.ruledata.service.export.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.tansun.rule.pojo.EnterPoint;
import com.tansun.rule.utils.MClass;
import com.tansun.ruledata.data.entity.DeployDatacomponent;
import com.tansun.ruledata.data.service.RuleDeployResourceService;
import com.tansun.ruledata.service.RuleFlowParamService;
import com.tansun.ruledata.service.export.RuleFlowParamExportService;

@Service("ruleFlowParamExportService")
public class RuleFlowParamExportServiceImpl implements RuleFlowParamExportService {
	
	@Autowired
	private  RuleFlowParamService  ruleFlowParamService;
	
	@Autowired
	private  RuleDeployResourceService  ruleDeployResourceService;

	@Override
	public String obtainFlowParamInfo(String projectCode, String invokePoint) {
		 Map<String, MClass>  map =ruleFlowParamService.findDeployRuleFlowParam(projectCode, invokePoint);
		return  JSON.toJSONString(map);
	}

	@Override
	public String obtainDatacommponetParamInfo(String projectCode, String invokePoint) {
		List<DeployDatacomponent> list = this.ruleFlowParamService.findDatacomponetParam(projectCode, invokePoint);
		return  JSON.toJSONString(list);
	}

	public RuleFlowParamService getRuleFlowParamService() {
		return ruleFlowParamService;
	}

	public void setRuleFlowParamService(RuleFlowParamService ruleFlowParamService) {
		this.ruleFlowParamService = ruleFlowParamService;
	}

	@Override
	public String obtainFlowParamInfo() {
		Map<String, List<EnterPoint>> all = ruleDeployResourceService.findAllEnterPoint();
		List<Object> result = new ArrayList<>();
		for(Map.Entry<String, List<EnterPoint>> entry:all.entrySet()){
			Map<String,Object> prj = new HashMap();
			List<EnterPoint> enters = entry.getValue();
			prj.put("projectKey", enters.get(0).getProjectCode());
			prj.put("projectName", enters.get(0).getName());
			List<Map<String,Object>> enterMaps = new ArrayList();
			for(EnterPoint enter:enters){
				Map<String,Object> enterMap = new HashMap<>();
				enterMap.put("enterName", enter.getName());
				enterMap.put("enterKey", enter.getCode());
				Map<String, MClass>  map =ruleFlowParamService.findDeployRuleFlowParam(enter.getProjectCode(), enter.getCode());
				enterMap.put("dataSets", map.values());
				enterMaps.add(enterMap);
			}
			prj.put("enters", enterMaps);
			result.add(prj);
		}
		
		return  JSON.toJSONString(result);
	}

}
